package cz.muni;

import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.restinterface.CustomerDTO;
import cz.muni.service.CustomerServiceLocal;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author OSiRiS-PC, Ondrej
 */
@WebServlet(urlPatterns = "/Rest/Customer/")
public class CustomerServlet extends HttpServlet {

    @EJB
    private CustomerServiceLocal customerService;
    private CustomerDTO customer;
    private List<CustomerDTO> customerList;
    private Long id;
    private String paramName;
    private Map<String, String[]> params;
    private ObjectMapper mapper;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        params = request.getParameterMap();

        for (Iterator<Entry<String, String[]>> it = params.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = it.next();
            String key = (String) entry.getKey();

            if (key.contains("findbyid")) {
                id = Long.valueOf(request.getParameter("findbyid"));
                customer = customerService.find(id);
                response.setContentType("application/json");
                mapper = new ObjectMapper();
                mapper.writeValue(response.getOutputStream(), customer);
            } else if (key.contains("findall")) {
                customerList = customerService.findAll();
                response.setContentType("application/json");
                mapper = new ObjectMapper();
                mapper.writeValue(response.getOutputStream(), customerList);
            } else if (key.contains("findByFirstName")) {
                String s = request.getParameter("findByFirstName");
                customerList = customerService.findByFirstName(s);
                response.setContentType("application/json");
                mapper = new ObjectMapper();
                mapper.writeValue(response.getOutputStream(), customerList);
            } else if (key.contains("findByLastName")) {
                String s = request.getParameter("findByLastName");
                customerList = customerService.findByLastName(s);
                response.setContentType("application/json");
                mapper = new ObjectMapper();
                mapper.writeValue(response.getOutputStream(), customerList);
            } else if (key.contains("findByPhoneNumber")) {
                String s = request.getParameter("findByPhoneNumber");
                customerList = customerService.findByPhoneNumber(s);
                response.setContentType("application/json");
                mapper = new ObjectMapper();
                mapper.writeValue(response.getOutputStream(), customerList);
            } else if (key.contains("findByEmail")) {
                String s = request.getParameter("findByEmail");
                customerList = customerService.findByEmail(s);
                response.setContentType("application/json");
                mapper = new ObjectMapper();
                mapper.writeValue(response.getOutputStream(), customerList);
            }
        }

    }

    @Override
    protected synchronized void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        params = request.getParameterMap();

        for (Entry<String, String[]> e : params.entrySet()) {
            if (e.getKey().contains("create")) {
                CustomerDTO c = new CustomerDTO();
                c.setFirstName(params.get("firstName")[0]);
                c.setLastName(params.get("lastName")[0]);
                c.setStreet(params.get("street")[0]);
                c.setStreetNumber(params.get("streetNumber")[0]);
                c.setCity(params.get("city")[0]);
                c.setPostalCode(params.get("postalCode")[0]);
                c.setPhoneNumber(params.get("phoneNumber")[0]);
                c.setEmail(params.get("email")[0]);
                customerService.create(c);
            } 
            else if (e.getKey().contains("update")) {
                CustomerDTO c = customerService.find(Long.valueOf(params.get("id")[0]));
                if (c != null) {
                    c.setFirstName(params.get("firstName")[0]);
                    c.setLastName(params.get("lastName")[0]);
                    c.setStreet(params.get("street")[0]);
                    c.setStreetNumber(params.get("streetNumber")[0]);
                    c.setCity(params.get("city")[0]);
                    c.setPostalCode(params.get("postalCode")[0]);
                    c.setPhoneNumber(params.get("phoneNumber")[0]);
                    c.setEmail(params.get("email")[0]);
                    customerService.update(c);
                }
            }
        }
    }

    @Override
    protected synchronized void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        params = request.getParameterMap();

        for (Iterator<Entry<String, String[]>> it = params.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = it.next();

            paramName = (String) entry.getKey();
            if (paramName.contains("delete")) {

                customer = customerService.find(Long.valueOf(request.getParameter("delete")));
                if (customer != null) {
                    customerService.delete(customer);
                }
            }

        }
    }

    @Override
    protected synchronized void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        params = request.getParameterMap();

        for (Iterator<Entry<String, String[]>> it = params.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = it.next();

            paramName = (String) entry.getKey();
            if (paramName.contains("update")) {

                mapper = new ObjectMapper();
                customer = mapper.readValue(request.getInputStream(), CustomerDTO.class);
                customerService.update(customer);
            }
        }
    }
}